package com.project.chat.pojo;

import com.project.chat.common.Role;
import lombok.Data;

import java.util.List;
import java.util.Random;

@Data
public class SettingTool {

    private int chiHuiId; //选择的词汇号
    private int pmNum; //平民数
    private int wdNum; //卧底数

    public static SettingTool createSettingTool(int chiHuiId, int pmNum, int wdNum){
        //保证合法创建
        if(pmNum >= 1 && wdNum >= 1){
            return new SettingTool(chiHuiId, pmNum, wdNum);
        }
        return null;
    }

    private SettingTool(int chiHuiId, int pmNum, int wdNum){
        this.chiHuiId = chiHuiId;
        this.pmNum = pmNum;
        this.wdNum = wdNum;
    };

    private SettingTool(){}

    //发牌
    public List<Person> faPai(List<Person> personList){
        if(personList == null) return null;
        if(personList.size() != (pmNum + wdNum)) return null;
        //创建词汇器
        ChiHuiTool chiHuiTool = ChiHuiTool.createChiHuiTool();
        //获取当前词汇
        ChiHui chiHui = chiHuiTool.getChiHui(chiHuiId);

        //根据现在的规则
        Random random = new Random();

        //卧底
        for(int i = 0; i < wdNum; i ++){
            //抽取几个人
            int r = random.nextInt(personList.size());
            Person p = personList.get(r);
            p.setType(Role.WODI.type);
            p.setChiHui(chiHui.getW());
        }
        //平民
        for(int i = 0; i < pmNum; i ++){
            Person p = personList.get(i);
            if(p.getType() == Role.PINGMIN.type) p.setChiHui(chiHui.getP());
        }

        return personList;
    }



}
